<?php 
class AttUtils {
  public static function student_period($myPdo, $student_id, $start, $end) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.student_id = :v1';
    $cond_vals->v = array(':v1' => $student_id);
    $enrolled_classes = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'student_enrolled_classes', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    
    $cond_vals->c = 't.date >= :v1 AND t.date <= :v2 AND t.student_id = :v3 AND t.is_tutorial = 0';
    $cond_vals->v = array(':v1' => $start, ':v2' => $end, ':v3' => $student_id);
    $absents_tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'attendences_absents', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    
    return AttUtils::_atdn($myPdo, $absents_tmp, $enrolled_classes);
  }
  
  public static function student_class_period($myPdo, $student_id, $class_id, $start, $end) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.student_id = :v1 AND t.class_id = :v2';
    $cond_vals->v = array(':v1' => $student_id, ':v2' => $class_id);
    $enrolled_classes = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'student_enrolled_classes', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    
    $cond_vals->c = 't.date >= :v1 AND t.date <= :v2 AND t.student_id = :v3 AND t.class_id = :v4 AND t.is_tutorial = 0';
    $cond_vals->v = array(':v1' => $start, ':v2' => $end, ':v3' => $student_id, ':v4' => $class_id);
    $absents_tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'attendences_absents', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    return AttUtils::_atdn($myPdo, $absents_tmp, $enrolled_classes);
  }
  
  public static function enrolled_program_period($myPdo, $enrolled_program_id, $start, $end) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.enrolled_program_id = :v1';
    $cond_vals->v = array(':v1' => $enrolled_program_id);
    $enrolled_classes = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'student_enrolled_classes', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    
    $cond_vals->c = 't.date >= :v1 AND t.date <= :v2 AND t.enrolled_program_id = :v3 AND t.is_tutorial = 0';
    $cond_vals->v = array(':v1' => $start, ':v2' => $end, ':v3' => $enrolled_program_id);
    $absents_tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'attendences_absents', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    return AttUtils::_atdn($myPdo, $absents_tmp, $enrolled_classes);
  }
  
  public static function enrolled_semester_period($myPdo, $enrolled_semester_id, $start, $end) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.enrolled_semester_id = :v1';
    $cond_vals->v = array(':v1' => $enrolled_semester_id);
    $enrolled_classes = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'student_enrolled_classes', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    
    $cond_vals->c = 't.date >= :v1 AND t.date <= :v2 AND t.enrolled_semester_id = :v3 AND t.is_tutorial = 0';
    $cond_vals->v = array(':v1' => $start, ':v2' => $end, ':v3' => $enrolled_semester_id);
    $absents_tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'attendences_absents', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    return AttUtils::_atdn($myPdo, $absents_tmp, $enrolled_classes);
  }
  
  public static function class_period($myPdo, $class_id, $start, $end) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.class_id = :v1';
    $cond_vals->v = array(':v1' => $class_id);
    $enrolled_classes = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'student_enrolled_classes', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    
    $cond_vals->c = 't.date >= :v1 AND t.date <= :v2 AND t.class_id = :v4 AND t.is_tutorial = 0';
    $cond_vals->v = array(':v1' => $start, ':v2' => $end, ':v4' => $class_id);
    $absents_tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'attendences_absents', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
    return AttUtils::_atdn($myPdo, $absents_tmp, $enrolled_classes);
  }
  
  public static function _atdn($myPdo, $absents_tmp, $enrolled_classes) {
    $absents = array();
    for($i=0; $i<count($absents_tmp); $i++){
      for($j=0; $j<count($enrolled_classes); $j++){
        if($absents_tmp[$i]->class_id == $enrolled_classes[$j]->class_id && (($enrolled_classes[$j]->status_id == STATUS_KEEP_RECORDS && strtotime($absents_tmp[$i]->date) < strtotime($enrolled_classes[$j]->wd_date)) || $enrolled_classes[$j]->status_id == STATUS_CLASS_ENROLLED)){
          array_push($absents,$absents_tmp[$i]);
        }
      }
    }
    $value = 0;
    $value2 = 0;
    foreach ($absents as $absent) {
      if ($absent->is_absent == 1 || $absent->is_late == 1) {
        $opts = new stdClass();
        $opts->select_expr = 't.id';
        $cond_vals = new stdClass();
        $cond_vals->c = 't.student_id = :v1 AND t.start <= :v2 AND t.end >= :v2';
        $cond_vals->v = array(':v1' => $absent->student_id, ':v2' => $absent->date);
        $tmp = count(DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'student_notes', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d);
        if ($tmp == 0) {
          if ($absent->is_absent == 1) {
            $value++;
          }
          if ($absent->is_late == 1) {
            $value2++;
          }
        }
      }
    }
    if (count($absents) == 0) {
      return -1;
    } 
    return round(((count($absents) - ($value + (int)($value2 / 2)))) / count($absents), 4);
  }
}
?>
